/**
 * https://leetcode.cn/problems/univalued-binary-tree/
 */
function isUnivalTree(root: TreeNode | null): boolean {
  let ans = true;

  let value = root!.val;
  let over = false;

  const preOrder = (node: TreeNode | null) => {
    if (over || node === null) return;

    if (node.val !== value) {
      over = true;
      ans = false;
      return;
    }

    preOrder(node.left);
    preOrder(node.right);
  };

  preOrder(root);

  return ans;
}
